// pages/tobeTzFirstStep/tobeTzFirstStep.js
let common_js = require("../../utils/common.js");
let url_js = require("../../utils/url.js");
let api = require("../../utils/apiRequest.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let const_js = require("../../utils/const.js");
let user_js = require("../../utils/user.js");
let notificationCenter = require('../../thridLib/WxNotificationCenter.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: null, //onload所携带的参数
    contentStatus: -1,
    shareShopInfo: null,
    submitInfo: {
      leaderShopId: '', //有推荐人的时候
      name: '',
      card: '',
      phone: '',
      wechat: '',
      bankNumbar: '',
      banKh: '',
      recommend: '',
      score: 0,
      isNextEnable: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.注册通知----首次进入，等code获取完成
    this.registNotification();
    // 3.初始化数据
    this.initData(options);
  },

  //注册通知，当自提点切换的时候，更新数据
  registNotification: function () {
    let that = this;
    notificationCenter.addNotification(const_js.getCodeSucessNoti, that.getCodeSuccess, that);
  },

  //微信获取code成功
  getCodeSuccess: function () {
    this.initTzShopData(this.data.options);
  },

  // 初始化数据
  initData(options) {
    // 团长二维码进来
    if (options.scene) {
      this.initTzShopData(options);
    } else {
      this.setData({
        contentStatus: 1
      });
    }
  },

  // 初始化团长自提点----团长分享码进来
  initTzShopData(options) {
    if (!options) return;

    // 1.
    this.setData({
      options: options
    });

    //2.请求code
    if (!user_js.getCode()) {
      api.getAccessToken();
    } else {
      let scene = decodeURIComponent(this.data.options.scene);
      let tmpArr = scene.split(',');
      let shopId = tmpArr[0];
      //获取shopInfo  
      this.getShopInfo(shopId);
    }
  },

  // 根据分享进来的团长id获取团长店铺信息
  getShopInfo(shopId) {
    let data = {
      id: shopId
    }
    api.wxRequest(url_js.shopInfoUrl, data).then(res => {
      this.setData({
        submitInfo: {
          ...this.data.submitInfo,
          ...{
            recommend: res.result.realName,
            leaderShopId: res.result.id
          }
        },
        shareShopInfo: res.result,
        contentStatus: 1
      })
    }).catch(err => {
      common_js.log(err);
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //移除通知
    let that = this;
    notificationCenter.removeNotification(const_js.getCodeSucessNoti, that);
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  //真实姓名
  onName: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.name.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.name.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.name = tmp;
    if (tmpSubmitInfo.score == 6) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //身份证号
  onCard: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.card.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.card.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.card = tmp;
    if (tmpSubmitInfo.score == 6) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //手机号
  onPhone: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.phone.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.phone.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.phone = tmp;
    if (tmpSubmitInfo.score == 6) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //微信号
  onWechat: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.wechat.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.wechat.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.wechat = tmp;
    if (tmpSubmitInfo.score == 6) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //银行卡号
  onBankNumber: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.bankNumbar.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.bankNumbar.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.bankNumbar = tmp;
    if (tmpSubmitInfo.score == 6) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //开户行名称
  onBankKh: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    if (tmpSubmitInfo.banKh.length == 0 && tmp.length > 0) {
      tmpSubmitInfo.score++;
    } else if (tmpSubmitInfo.banKh.length > 0 && tmp.length == 0) {
      tmpSubmitInfo.score--;
    }
    tmpSubmitInfo.banKh = tmp;
    if (tmpSubmitInfo.score == 6) {
      tmpSubmitInfo.isNextEnable = true;
    } else {
      tmpSubmitInfo.isNextEnable = false;
    }
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //您的推荐人
  onRecommed: function (e) {
    let tmp = e.detail.content;
    let tmpSubmitInfo = this.data.submitInfo;
    tmpSubmitInfo.recommend = tmp;
    this.setData({
      submitInfo: tmpSubmitInfo
    });
  },

  //下一步
  onNext: function (e) {
    //1.是否可以点击下一步
    if (!this.data.submitInfo.isNextEnable) return;
    //2.身份证号位数
    if (this.data.submitInfo.card.length != 18) {
      common_js.showToast('请输入正确的身份证号!');
      return;
    }
    //3.手机号
    if (!common_js.isPhone(this.data.submitInfo.phone)) {
      common_js.showToast('请输入正确的手机号!');
      return;
    }
    //4.校验微信号是否含有汉字
    if (common_js.containsChinese(this.data.submitInfo.wechat)) {
      common_js.showToast('微信号不能含有汉字!');
      return;
    }
    //5.银行卡号
    if (this.data.submitInfo.bankNumbar.length == 0) {
      common_js.showToast('请输入正确的银行卡号!');
      return;
    }
    //6.跳转
    wx.navigateTo({
      url: '../tobeTzSecondStep/tobeTzSecondStep?dzInfo=' + common_js.jsonStringify(this.data.submitInfo)
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.tobeTzFirstStepDone);
  }
})